package org.jeecg.modules.power.user.controller;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.power.group.entity.GroupUser;
import org.jeecg.modules.power.group.service.IYlGroupService;
import org.jeecg.modules.power.order.service.IYlMemberOrderService;
import org.jeecg.modules.power.order.service.IYlPowerBankOrderService;
import org.jeecg.modules.power.user.service.IYlUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: jeecg-boot-parent
 * @BelongsPackage: org.jeecg.modules.power.user.controller
 * @Author: mi
 * @CreateTime: 2024-07-18 09:36
 * @Description:
 */
@Api(tags="首页相关接口")
@RestController
@RequestMapping("/power/home")
@Slf4j
public class HomeController {

    @Autowired
    private IYlUserService ylUserService;


    @Autowired
    private IYlMemberOrderService ylMemberOrderService;

    //小组
    @Autowired
    private   IYlGroupService ylGroupService;

    //租借订单
    @Autowired
    private  IYlPowerBankOrderService ylPowerBankOrderService;

    //首页统计

    @GetMapping("getHomeStatistics")
    public Result<?> getHomeStatistics() {
        //取出 登陆用户
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();


        //获取今日 本月 所有用户 总数
        Map<String,Object> returnMap = new HashMap<>();
        Map<String, Object> allUser=ylUserService.getHomeUser(0);//所有
        returnMap.put("allUserWx",oConvertUtils.isEmpty(allUser.get("wxQuantity"))?"0":allUser.get("wxQuantity"));//微信
        returnMap.put("allUserAli", oConvertUtils.isEmpty(allUser.get("aliQuantity"))?"0":allUser.get("aliQuantity"));//支付宝
        returnMap.put("allUser",allUser.get("quantity"));//所有

        Map<String, Object> dayUser= ylUserService.getHomeUser(1);//今日
        returnMap.put("dayUserWx",oConvertUtils.isEmpty(dayUser.get("wxQuantity"))?"0":dayUser.get("wxQuantity"));//微信
        returnMap.put("dayUserAli", oConvertUtils.isEmpty(dayUser.get("aliQuantity"))?"0":dayUser.get("aliQuantity"));//支付宝
        returnMap.put("dayUser",dayUser.get("quantity"));//所有
        Map<String, Object> yueUser=ylUserService.getHomeUser(2);//本月
        returnMap.put("yueUserWx",oConvertUtils.isEmpty(yueUser.get("wxQuantity"))?"0":yueUser.get("wxQuantity"));//微信
        returnMap.put("yueUserAli", oConvertUtils.isEmpty(yueUser.get("aliQuantity"))?"0":yueUser.get("aliQuantity"));//支付宝
        returnMap.put("yueUser",yueUser.get("quantity"));//所有



        //统计租借订单 判定是不是业务员

        GroupUser groupUser = ylGroupService.getUserGroup(sysUser.getId());
        if (oConvertUtils.isNotEmpty(groupUser)){//说明是业务员
            List<String>stringList=new LinkedList<>();
            //判断是不是组长 是否组长1是2否
            if (groupUser.getIsLeader().equals("1")){
                stringList=ylGroupService.getGroupUserById(groupUser.getGroupId());
            }else {
                //根据群组id 获取数据id
                stringList.add(groupUser.getUserId());
            }
            returnMap.put("allPowerBankOrder",ylPowerBankOrderService.getHomePowerBankOrder(stringList,0));//所有
            returnMap.put("dayPowerBankOrder",ylPowerBankOrderService.getHomePowerBankOrder(stringList,1));//今日
            returnMap.put("yuePowerBankOrder",ylPowerBankOrderService.getHomePowerBankOrder(stringList,4));//本月

            //统计充值会员 累计会员充值数 今日 本月
            Map<String, Object> allMemberOrder=  ylMemberOrderService.getHomeMemberOrder(stringList,0);//所有
            returnMap.put("allMemberOrder",allMemberOrder);//所有
            Map<String, Object> dayMemberOrder=  ylMemberOrderService.getHomeMemberOrder(stringList,1);//今日
            returnMap.put("dayMemberOrder",dayMemberOrder);//所有
            Map<String, Object> yueMemberOrder=  ylMemberOrderService.getHomeMemberOrder(stringList,2);//本月
            returnMap.put("yueMemberOrder",yueMemberOrder);//所有

        }else {
            //统计充值会员 累计会员充值数 今日 本月
            Map<String, Object> allMemberOrder=  ylMemberOrderService.getHomeMemberOrder(new LinkedList<>(),0);//所有
            returnMap.put("allMemberOrder",allMemberOrder);//所有
            Map<String, Object> dayMemberOrder=  ylMemberOrderService.getHomeMemberOrder(new LinkedList<>(),1);//今日
            returnMap.put("dayMemberOrder",dayMemberOrder);//所有
            Map<String, Object> yueMemberOrder=  ylMemberOrderService.getHomeMemberOrder(new LinkedList<>(),2);//本月
            returnMap.put("yueMemberOrder",yueMemberOrder);//所有
            //不是业务员 查全部订单 统计金额
            returnMap.put("allPowerBankOrder",ylPowerBankOrderService.getHomePowerBankOrder(new LinkedList<>(),0));//所有
            returnMap.put("dayPowerBankOrder",ylPowerBankOrderService.getHomePowerBankOrder(new LinkedList<>(),1));//今日
            returnMap.put("yuePowerBankOrder",ylPowerBankOrderService.getHomePowerBankOrder(new LinkedList<>(),4));//本月
        }




//        System.out.println(returnMap);









        return Result.OK(returnMap);
    }













}
